package com.java.web;

import com.java.bean.Student;
import com.java.service.StudentService;
import com.java.service.impl.StudentServiceImpl;
import com.java.utils.ResponseUtils;
import com.sun.nio.file.ExtendedWatchEventModifier;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: wjr
 * @Description:
 * @Date Created in  2020-09-06 4:40
 * @Modified By:
 */
@WebServlet(value = "/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = new StudentServiceImpl();
        //在首页通过输入id删除的方式
        /*int studentno = Integer.parseInt(req.getParameter("studentno"));
        Student student = studentService.selectStudentById(studentno);*/

        //在index.jsp删除的方式
        int studentId = Integer.parseInt(req.getParameter("studentId"));
        Student student = studentService.selectStudentById(studentId);

        //提示信息
        String msg = "";
        if (student != null){
            boolean b = studentService.deleteStudent(student);
            if (b){
                msg="删除成功,该学生id为"+student.getStudentId()+",姓名为"+student.getStuName();
            }else {
                msg="删除失败";
            }
        }else {
            msg="该学生不存在";
        }

        //发送提示信息
        ResponseUtils responseUtils = new ResponseUtils();
        responseUtils.ResponseMsg(resp,msg,"/showAll");
    }
}
